import logging
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from page_object.analysetools import AnalyseTools
from tools.base_page import BasePage


class ZhibiaokuCat(BasePage):

    def option_search(self, text, type):
        '''
        :param text: send_keys 文本值
        :param type: 类型的key值比如指标名称是zhibiao_code
        :return:
        '''
        self.elementclickintercepted(self.only_read_yaml('../data/zhibiaoku_cat.yaml', 'type_option'))
        time.sleep(2)
        self.verify_wait_click(self.only_read_yaml('../data/zhibiaoku_cat.yaml', type))
        self.open_yaml_element('../data/zhibiaoku_cat.yaml', type)
        self.open_yaml_element('../data/zhibiaoku_cat.yaml', 'search_input', content=text)
        time.sleep(2)
        click_locator = self.only_read_yaml('../data/zhibiaoku_cat.yaml', 'seacher_click_locator')
        ele = self.find(click_locator)
        self.verify_wait_click(click_locator)
        self.driver.execute_script("arguments[0].click();",ele)
        #self.open_yaml_element('../data/zhibiaoku_cat.yaml', 'seacher_click_locator')
        time.sleep(2)
        return self

    def verify_search_res(self):

        zhibiao_name_res = self.only_read_yaml('../data/zhibiaoku_cat.yaml', 'zhibiao_name_res')
        status = self.is_element(zhibiao_name_res)
        if status is True:
            return status
        else:
            res_total = self.only_read_yaml('../data/zhibiaoku_cat.yaml', 'search_res_total')
            total_text = self.find(res_total).text
            if total_text == '共 0 条':
                logging.info(f'无搜索结果,当前搜索结果为{total_text}')
                return True
            else:
                logging.info('程序报错了 你再看看？')
                return False

    def cat_zhibiao(self):
        operate_locator = self.only_read_yaml('../data/target_house_cat.yaml', 'operate_locator')
        time.sleep(2)
        self.verify_wait_click(operate_locator)
        ActionChains(self.driver).move_to_element(self.find(operate_locator)).perform()  #  鼠标放到操作按钮上
        self.open_yaml_element('../data/target_house_cat.yaml', 'cat_zhibiao')
        time.sleep(2)
        return self.is_element(self.only_read_yaml('../data/target_house_cat.yaml', 'rcDialogTitle'))

    def analyse_zhibiao(self):  # 分析指标
        operate_locator = self.only_read_yaml('../data/target_house_cat.yaml', 'operate_locator')
        self.verify_wait_click(operate_locator)
        try:
            ActionChains(self.driver).click_and_hold(self.find(operate_locator)).perform()  # 鼠标放到操作按钮上
        except:
            ActionChains(self.driver).click_and_hold(self.find(operate_locator)).perform()  # 鼠标放到操作按钮上
            try:
                ActionChains(self.driver).click_and_hold(self.find(operate_locator)).perform()  # 鼠标放到操作按钮上
            except:
                ActionChains(self.driver).click_and_hold(self.find(operate_locator)).perform()  # 鼠标放到操作按钮上
        self.open_yaml_element('../data/target_house_cat.yaml', 'analyse_zhiiao')
        return AnalyseTools(self.driver)

    def batch_run_data(self):# 批量重跑数据
        locator = self.only_read_yaml('../data/target_house_cat.yaml','betch_run')
        self.elementclickintercepted(locator)
        self.open_yaml_element('../data/target_house_cat.yaml','run_input',content='出生日期')
        self.open_yaml_element('../data/target_house_cat.yaml','batch_run_search')
        time.sleep(2)
        self.open_yaml_element('../data/target_house_cat.yaml','batch_run_option',is_elements=1,ele_num=1)
        remove_locator = self.only_read_yaml('../data/target_house_cat.yaml','remove_locator')[1]
        starttime_locator = self.only_read_yaml('../data/target_house_cat.yaml','starttime_locator')[1]
        endtime_locator = self.only_read_yaml('../data/target_house_cat.yaml','endtime_locator')[1]
        self.exist_readonly_view(remove_locator,starttime_locator,endtime_locator)
        time.sleep(2)
        self.open_yaml_element('../data/target_house_cat.yaml','anew_run_confirm',is_elements=1,ele_num=2)
        return self


    def verify_whether_data_batchrun_succeed(self):
        #  验证数据是否重跑成功
        zhibiaoku_list_status = self.only_read_yaml('../data/target_house_cat.yaml','zhibiaoku_list_status')
        self.verify_wait_click(zhibiaoku_list_status)
        status_text = self.verify_wait_click(zhibiaoku_list_status).text
        return status_text

    def apply_newly(self,code,name,ms):  # 申请新增


        self.elementclickintercepted(self.only_read_yaml('../data/target_house_cat.yaml','apply_newly'))
        self.open_yaml_element('../data/target_house_cat.yaml','yewu_code',content=code)
        self.open_yaml_element('../data/target_house_cat.yaml','zhibiao_name',content=name)
        self.open_yaml_element('../data/target_house_cat.yaml','zhibiao_ms',content=ms)


        self.open_yaml_element('../data/target_house_cat.yaml','shangji_zb')
        js = "document.querySelector('body>div:nth-of-type(4)>div>div>div>ul').scrollTop = 100000"
        time.sleep(2)
        self.driver.execute_script(js)

        sjbz_option = "body>div:nth-of-type(4)>div>div>div>ul>li:nth-of-type(14)" # 上级指标 选项
        time.sleep(2)
        self.selector_execute_script(sjbz_option)
        time.sleep(2)

        self.open_yaml_element('../data/target_house_cat.yaml','zbc')
        self.open_yaml_element('../data/target_house_cat.yaml','zbc')

        zbc_option = 'body>div:nth-of-type(5)>div>div>div>ul>li:nth-of-type(1)'  # 指标簇选项
        self.selector_execute_script(zbc_option)
        time.sleep(2)



        ele = self.find(('css selector','.ant-modal-body>div>button:nth-child(2)'))
        self.driver.execute_script("arguments[0].scrollIntoView();", ele)
        time.sleep(2)  # 等待几秒查看滚动条是否下滑

        # 这里由于点完了一级标签中的选项，点开的选项会遮挡住二级标签元素。所以需要点击一下其他地方收起掉选项
        self.open_yaml_element('../data/target_house_cat.yaml', 'zbc_title')



        self.open_yaml_element('../data/target_house_cat.yaml','first_bq')
        first_bq_option = "body>div:nth-of-type(6)>div>div>div>ul>li:nth-of-type(1)"  # 一级标签选项
        self.selector_execute_script(first_bq_option)
        time.sleep(2)



        self.open_yaml_element('../data/target_house_cat.yaml','ej_bq')  # 二级标签
        ejbq_option = "body>div:nth-of-type(7)>div>div>div>ul>li:nth-of-type(1)"  # 二级标签选项
        self.selector_execute_script(ejbq_option)

        time.sleep(2)
        self.open_yaml_element('../data/target_house_cat.yaml', 'zbc_title')
        ele.click()
        return self

    def verify_newly_succssful(self,text):
        # 验证是否添加成功
        self.option_search(text,'zhibiao_code_news')
        code_locator = self.only_read_yaml('../data/zhibiaoku_cat.yaml','search_first_code')
        return self.find(code_locator).text

